_package("alz.mui");

_import("alz.mui.Component");

/**
 * 模态对话框使用的遮挡面板组件
 */
_class("ModalPanel", Component, function(){
	this._init = function(){
		_super._init.call(this);
		this._targetStack = [];
		this._activeTarget = null;
		this._iframe = null;  //用来遮挡SELECT等DIV遮挡不住的组件
		this._panel = null;    //再用这个DIV遮挡在IFRAME上面
	};
	this.create = function(parent){
		this.setParent(parent);
		var obj = this._createElement("div");
		obj.className = "wui-ModalPanel";
		if(parent){
			parent.getContainer().appendChild(obj);
		}
		this.init(obj);
		return obj;
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		this.moveTo(0, 0);
		this.setOpacity(0.6);
		if(runtime.ie){
			this._iframe = this._createElement("iframe");
			this._iframe.setAttribute("scrolling", "no");
			this._iframe.setAttribute("frameBorder", "0");
			this._iframe.setAttribute("frameSpacing", "0");
			//this._iframe.setAttribute("allowTransparency", "true");
			this._iframe.style.display = "none";
			this._iframe.src = "about:blank";
			this._self.appendChild(this._iframe);
		}
		this._panel = this._createElement("div");
		this._panel.style.display = "none";
		this._self.appendChild(this._panel);
	};
	this.dispose = function(){
		if(this._disposed) return;
		this._panel = null;
		this._iframe = null;
		this._activeTarget = null;
		for(var i = 0, len = this._targetStack.length; i < len; i++){
			this._targetStack[i] = null;
		}
		this._targetStack = [];
		_super.dispose.apply(this);
	};
	this.setVisible = function(v){
		_super.setVisible.apply(this, arguments);
		if(this._iframe){
			this._iframe.style.display = v ? "" : "none";
		}
		this._panel.style.display = v ? "" : "none";
		if(this._visible){  //如果面板已经显示
			//this._activeTarget.setVisible(false);
		}else{
		}
	};
	this.resize = function(w, h){
		_super.resize.apply(this, arguments);
		this._panel.style.width = w + "px";
		this._panel.style.height = h + "px";
		//this._activeTarget.moveToCenter();  //居中对话框
	};
	this.pushTarget = function(v){
		this._activeTarget = v;
		this._targetStack.push(v);
		var rect = this._parent.getViewPort();
		//var rect = runtime._workspace.getViewPort();
		this.resize(rect.w, rect.h);
		//var screen = runtime.getBody();
		/* 是否需要移动呢？
		var rect = this._parent.getViewPort();
		this.moveTo(rect.x, rect.y);
		*/
		//this.resize(screen.clientWidth, screen.clientHeight);
		//this.resize(
		//	Math.max(screen.scrollWidth, screen.clientWidth),
		//	Math.max(screen.scrollHeight, screen.clientHeight)
		//);
		//screen = null;
		this.setVisible(true);  //!!v
		this.setZIndex(runtime.getNextZIndex());
	};
	this.checkPrivousVisible = function(){
		var len = this._targetStack.length;
		if(len > 1){
			var target = this._targetStack[len - 2];
			if(!target.getVisible()){
				//[TODO]隐藏连续多个复用模式的对话框时，此刻的显示时机太晚了
				target.setZIndex(this.getZIndex() - 1);
				target.setVisible(true);
				//target.moveToCenter();
			}
			target = null;
		}
	};
	this.popTarget = function(v){
		this._targetStack.pop();
		var len = this._targetStack.length;
		if(len > 0){
			var target = this._activeTarget = this._targetStack[len - 1];
			if(target){
				target.setZIndex(runtime.getNextZIndex());
				if(!target.getVisible()){
					//[TODO]隐藏连续多个复用模式的对话框时，此刻的显示时机太晚了
					target.setVisible(true);
					//target.moveToCenter();
				}
			}
		}else{
			this._activeTarget = null;
			this.setVisible(false);
		}
	};
	this.getActiveTarget = function(){
		return this._activeTarget;  //this._targetStack[this._targetStack.length - 1];
	};
	/**
	 * 替换模态面板关联的目标组件，其间runtime._zIndex数值不会增量
	 */
	this.replaceTarget = function(target){
		var top = this._targetStack.pop();
		this._targetStack.push(target);
		top.setVisible(false);
		target.setVisible(true);
		target.setZIndex(top.getZIndex());
	};
});